package com.sugar.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

/**
 * @author felix
 * @date 2025/2/10 11:05
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws Exception {
        Class<?> clazz = Class.forName("com.sugar.reflection.Student");
        Constructor<?> con = clazz.getDeclaredConstructor(String.class,int.class);
        //临时修改构造方法的权限
        con.setAccessible(true);
        Student student = (Student) con.newInstance("zhansan",18);
        System.out.println(student);

        Field[] fields = clazz.getFields();
        for(Field field : fields)
            System.out.println(field);

        Field[] fields2 = clazz.getDeclaredFields();
        for(Field field : fields2)
            System.out.println(field);

        System.out.println("====================");
        Field name1 = clazz.getDeclaredField("name");
        System.out.println(name1);

        Field name = clazz.getField("name");
        System.out.println(name);
    }
}
